package cn.iocoder.yudao.module.design.controller.admin.purchase;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.design.controller.admin.purchase.vo.*;
import cn.iocoder.yudao.module.design.convert.purchase.PurchaseConvert;
import cn.iocoder.yudao.module.design.dal.dataobject.pic.PicDO;
import cn.iocoder.yudao.module.design.dal.dataobject.purchase.PurchaseDO;
import cn.iocoder.yudao.module.design.enums.design.PicSourceTypeEnum;
import cn.iocoder.yudao.module.design.service.pic.PicService;
import cn.iocoder.yudao.module.design.service.purchase.PurchaseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 采购清单")
@RestController
@RequestMapping("/design/purchase")
@Validated
public class PurchaseController {

    @Resource
    private PurchaseService purchaseService;

    @Resource
    private PicService picService;

    @PostMapping("/create")
    @Operation(summary ="创建采购清单")
    @PreAuthorize("@ss.hasPermission('design:purchase:create')")
    public CommonResult<Long> createPurchase(@Valid @RequestBody PurchaseCreateReqVO createReqVO) {
        return success(purchaseService.createPurchase(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary ="更新采购清单")
    @PreAuthorize("@ss.hasPermission('design:purchase:update')")
    public CommonResult<Boolean> updatePurchase(@Valid @RequestBody PurchaseUpdateReqVO updateReqVO) {
        purchaseService.updatePurchase(updateReqVO);
        return success(true);
    }
    @PutMapping("/updatePurchaseStatus")
    @Operation(summary ="更新采购清单")
    @PreAuthorize("@ss.hasPermission('design:purchase:update')")
    public CommonResult<Boolean> updatePurchaseStatus(@Valid @RequestBody PurchaseUpdateReqVO updateReqVO) {
        purchaseService.updatePurchase(updateReqVO);
        return success(true);
    }
    @DeleteMapping("/delete")
    @Operation(summary ="删除采购清单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('design:purchase:delete')")
    public CommonResult<Boolean> deletePurchase(@RequestParam("id") Long id) {
        purchaseService.deletePurchase(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary ="获得采购清单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('design:purchase:query')")
    public CommonResult<PurchaseRespVO> getPurchase(@RequestParam("id") Long id) {
        PurchaseDO purchase = purchaseService.getPurchase(id);

        PicDO picDO = new PicDO();
        picDO.setHouseId(purchase.getHouseId());
        picDO.setFlowId(purchase.getFlowId());
        picDO.setItemId(purchase.getId());
        picDO.setUploadType("0");
        picDO.setStatus("0");
        List<PicDO> picData = picService.getPicData(picDO);
        // 查询当前item 所有附件
        Map<String, List<PicDO>> collect = picData.stream()
                .collect(Collectors.groupingBy(item -> item.getSourceType()));
        PurchaseRespVO convert = PurchaseConvert.INSTANCE.convert(purchase);
        // 保存附件
        convert.setFileList(collect.get(PicSourceTypeEnum.PIC_SOURCE_TYPE_ENUM_010.getCode()));
        // 保存合同
        convert.setContractFile(collect.get(PicSourceTypeEnum.PIC_SOURCE_TYPE_ENUM_011.getCode()));
        // 保存图纸
        convert.setDrawList(collect.get(PicSourceTypeEnum.PIC_SOURCE_TYPE_ENUM_012.getCode()));
        // 保存清单清单
        convert.setPurchaseList(collect.get(PicSourceTypeEnum.PIC_SOURCE_TYPE_ENUM_013.getCode()));
        return success(convert);
    }

    @GetMapping("/list")
    @Operation(summary ="获得采购清单列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('design:purchase:query')")
    public CommonResult<List<PurchaseRespVO>> getPurchaseList(@RequestParam("ids") Collection<Long> ids) {
        List<PurchaseDO> list = purchaseService.getPurchaseList(ids);
        return success(PurchaseConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary ="获得采购清单分页")
    @PreAuthorize("@ss.hasPermission('design:purchase:query')")
    public CommonResult<PageResult<PurchaseRespVO>> getPurchasePage(@Valid PurchasePageReqVO pageVO) {
        PageResult<PurchaseDO> pageResult = purchaseService.getPurchasePage(pageVO);
        return success(PurchaseConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary ="导出采购清单 Excel")
    @PreAuthorize("@ss.hasPermission('design:purchase:export')")
   
    public void exportPurchaseExcel(@Valid PurchaseExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PurchaseDO> list = purchaseService.getPurchaseList(exportReqVO);
        // 导出 Excel
        List<PurchaseExcelVO> datas = PurchaseConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "采购清单.xls", "数据", PurchaseExcelVO.class, datas);
    }

    @PostMapping("/getPurchaseListByFlowId")
    @Operation(summary ="获得采购清单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('design:purchase:query')")
    public CommonResult< List<Map>> getPurchaseListByFlowId(@RequestBody PurchaseQueryVO purchaseQueryVO) {
        List<Map> purchase = purchaseService.getPurchaseListByFlowId(purchaseQueryVO.getFlowId(),purchaseQueryVO.getStatusCd());
        return success( purchase);
    }


    @PostMapping("/getPurchaseListInfoByFlowId")
    @Operation(summary ="获得采购清单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('design:purchase:query')")
    public CommonResult< Map> getPurchaseListInfoByFlowId(@RequestBody PurchaseQueryVO purchaseQueryVO) {
        Map result = purchaseService.getPurchaseListInfoByFlowId(purchaseQueryVO.getFlowId(),purchaseQueryVO.getStatusCd());
        return success( result);
    }

    @GetMapping("/modifyPurchaseStatusCd")
    @Operation(summary ="修改货物状态")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('design:purchase:update')")
    public CommonResult<Boolean> modifyPurchaseStatusCd(@RequestParam("id") Long id) {
        purchaseService.modifyPurchaseStatusCd(id);
        return success( true);
    }

//    public CommonResult< Map> addPurchaseInfoByFlowId(@RequestBody PurchaseAddVO purchaseAddVO) {
//        Map result = purchaseService.getPurchaseListInfoByFlowId(purchaseQueryVO.getFlowId(),purchaseQueryVO.getStatusCd());
//        return success( result);
//    }
}
